﻿#include "HnrjitWigdet.h"
#include "HnrjitImaging.h"

CHnrjitWigdet::CHnrjitWigdet()
	: m_dwCtrlid(0)
	, m_isEnabled(TRUE)
	, m_isVisible(TRUE)
	, m_isTransparent(FALSE)
	, m_rcGeometry(0, 0, 0, 0)
{
#ifdef _DEBUG
	m_dwThreadID = GetCurrentThreadId();
#endif // _DEBUG
}

CHnrjitWigdet::~CHnrjitWigdet()
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
}

void CHnrjitWigdet::SetAttribute(const TiXmlElement* pxElement, HZIP /*hZipRes = NULL*/)
{
	const CHAR* pszValue;
	if (pszValue = pxElement->Attribute("Ctrlid"))
	{
		DWORD dwCtrlid = strtol(pszValue, NULL, 10);
		SetCtrlid(dwCtrlid);
	}
	if (pszValue = pxElement->Attribute("Name"))
	{
		CAtlArray<TCHAR> caResult;
		const TCHAR* pszName = CHnrjitImaging::Utf8ToLocal(pszValue, &caResult);
		if (pszName) SetName(pszName);
	}
	if (pszValue = pxElement->Attribute("Geometry"))
	{
		CHAR* pszEndPtr = NULL;
		LONG lX = strtol(pszValue, &pszEndPtr, 10); ASSERT(pszEndPtr);
		if (pszEndPtr)
		{
			LONG lY = strtol(pszEndPtr + 1, &pszEndPtr, 10); ASSERT(pszEndPtr);
			if (pszEndPtr)
			{
				LONG lWidth = strtol(pszEndPtr + 1, &pszEndPtr, 10); ASSERT(pszEndPtr);
				if (pszEndPtr)
				{
					LONG lHeight = strtol(pszEndPtr + 1, NULL, 10);
					SetGeometry(lX, lY, lWidth, lHeight);
				}
			}
		}
	}
	if (pszValue = pxElement->Attribute("Enabled"))
	{
		if (strcmp(pszValue, "false") == 0)
		{
			SetEnabled(FALSE);
		}
	}
	if (pszValue = pxElement->Attribute("Visible"))
	{
		if (strcmp(pszValue, "false") == 0)
		{
			SetVisible(FALSE);
		}
	}
	if (pszValue = pxElement->Attribute("Transparent"))
	{
		if (strcmp(pszValue, "true") == 0)
		{
			SetTransparent(TRUE);
		}
	}
}

DWORD CHnrjitWigdet::GetCtrlid() const
{
	return m_dwCtrlid;
}

void CHnrjitWigdet::SetCtrlid(DWORD dwCtrlid)
{
	m_dwCtrlid = dwCtrlid;
}

const CAtlString& CHnrjitWigdet::GetName() const
{
	return m_strName;
}

void CHnrjitWigdet::SetName(const TCHAR* pszName)
{
	m_strName = pszName;
}

CPoint CHnrjitWigdet::GetLocation() const
{
	return m_rcGeometry.TopLeft();
}

void CHnrjitWigdet::SetLocation(const CPoint& ptLocation)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (GetLocation() != ptLocation)
	{
		Invalidate(m_rcGeometry);
		m_rcGeometry.MoveToXY(ptLocation);
		Invalidate(m_rcGeometry);
	}
}

void CHnrjitWigdet::SetLocation(int nX, int nY)
{
	SetLocation(CPoint(nX, nY));
}

CSize CHnrjitWigdet::GetSize() const
{
	return CSize(m_rcGeometry.Width(), m_rcGeometry.Height());
}

void CHnrjitWigdet::SetSize(const CSize& tagSize)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (GetSize() != tagSize)
	{
		Invalidate(m_rcGeometry);
		m_rcGeometry = CRect(GetLocation(), tagSize);
		Invalidate(m_rcGeometry);
	}
}

void CHnrjitWigdet::SetSize(int nWidth, int nHeight)
{
	SetSize(CSize(nWidth, nHeight));
}

const CRect& CHnrjitWigdet::GetGeometry() const
{
	return m_rcGeometry;
}

void CHnrjitWigdet::SetGeometry(CPoint ptLocation, CSize tagSize)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	CRect rcGeometry = CRect(ptLocation, tagSize);
	if (m_rcGeometry != rcGeometry)
	{
		Invalidate(m_rcGeometry);
		m_rcGeometry = rcGeometry;
		Invalidate(m_rcGeometry);
	}
}

void CHnrjitWigdet::SetGeometry(int nX, int nY, int nWidth, int nHeight)
{
	SetGeometry(CPoint(nX, nY), CSize(nWidth, nHeight));
}

BOOL CHnrjitWigdet::IsEnabled() const
{
	return m_isEnabled;
}

void CHnrjitWigdet::SetEnabled(BOOL isEnabled)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (m_isEnabled != isEnabled)
	{
		m_isEnabled = isEnabled;
		Invalidate(m_rcGeometry);
	}
}

BOOL CHnrjitWigdet::IsVisible() const
{
	return m_isVisible;
}

void CHnrjitWigdet::SetVisible(BOOL isVisible)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (m_isVisible != isVisible)
	{
		m_isVisible = isVisible;
		Invalidate(m_rcGeometry);
	}
}

BOOL CHnrjitWigdet::IsTransparent() const
{
	return m_isTransparent;
}

void CHnrjitWigdet::SetTransparent(BOOL isTransparent)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	if (m_isTransparent != isTransparent)
	{
		m_isTransparent = isTransparent;
		Invalidate(m_rcGeometry);
	}
}
